<template>
  <div>
    <div class="px-4 pt-1 bg-white">
      <div class="my-1 flex items-center">
        <span class="pl-3"> 选择操作实体： </span>
        <el-radio-group v-model="CurrEntityName" label="" size="default">
          <el-radio-button v-for="(item, index) in EntityNames" :label="item" :model-value="item" @change="EntityChange">
            {{ item }}
          </el-radio-button>
        </el-radio-group>
      </div>
      <hr class="my-1" />
      <!--      代码生成-操作区域-->
      <div class="mt-2 mb-1 flex justify-end items-center">
        <!--        <div class="pl-3">-->
        <!--          <el-button plain @click="SaveEntityInfo" type="primary">保存配置</el-button>-->
        <!--        </div>-->
        <div class="pl-3">
          <el-button plain type="warning" @click="GenFile">保存并生成代码</el-button>
        </div>

        <!--        <div class="flex pl-3 items-center">-->
        <!--          <label>代码生成：</label>-->
        <!--          <el-button plain type="warning">dbdto文件</el-button>-->
        <!--          <el-button plain type="warning">控制器</el-button>-->
        <!--          <el-button plain type="warning">service</el-button>-->
        <!--          <el-button plain type="danger">service</el-button>-->
        <!--        </div>-->
      </div>
      <hr class="my-1" />
      <!--    <div class="my-1 flex items-center" v-if="CurrFieldList.length > 0">-->
      <!--      <span class="pl-3" > 操作字段：</span>-->
      <!--      <el-radio-group v-model="CurrFieldValue" size="small" label="">-->
      <!--        <el-radio-button type="info" @change="FieldChange" :label="item.label" v-for="(item,index) in CurrFieldList"-->
      <!--                         :model-value="item">-->
      <!--          {{item.field_name}}-->
      <!--        </el-radio-button>-->
      <!--      </el-radio-group>-->
      <!--    </div>-->
      <!--    <hr class="my-1">-->

      <ProTable
        v-if="CurrFieldList.length > 0"
        ref="proTableRef"
        :columns="ColumnsGeneration"
        :data="CurrFieldList"
        :indent="20"
        :toolButton="false"
        row-key="path"
        title="菜单列表"
      >
        <!-- 菜单操作 -->
        <template #operation="scope">
          <el-button :icon="EditPen" link type="primary" @click="EditFieldClick(scope.row)"> 配置字段</el-button>
        </template>
      </ProTable>
    </div>

    <EditField ref="refEditField" @fieldUpdate="updateFieldParam"></EditField>
  </div>
</template>
<script lang="tsx" setup>
import { computed, onMounted, ref, watch } from "vue";
import { HttpSendGenEntityInfo, HttpSendGenEntityNames, HttpSendGenEntitySave, HttpSendGenFile } from "@/api/modules/generation";
import { ResultData } from "@/api/interface";
import { RespOk } from "@/constant/codes";
import { EntityInfo } from "@/api/interface/Generation/EntityInfo";
import { ReqEntityInfo } from "@/api/interface/Generation/ReqEntityInfo";
import { FieldInfo } from "@/api/interface/Generation/FieldInfo";
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps } from "@/components/ProTable/interface";
import { Delete, DocumentAdd, EditPen } from "@element-plus/icons-vue";
import { ColumnsGeneration } from "@/views/generation/columns";
import { useStoreGeneration } from "@/views/generation/store";
import { ElMessage } from "element-plus";
import EditField from "@/views/generation/EditField.vue";

let CurrEntityName = ref("");
let CurrEntityInfo = ref<EntityInfo>({} as EntityInfo);

let EntityNames = ref<string[]>([]);
let CurrFieldList = ref<FieldInfo[]>([]);

onMounted(() => {
  GetEntityNames();
});

const SaveEntityInfo = () => {
  HttpSendGenEntitySave(CurrEntityInfo.value).then((res: ResultData<any>) => {
    if (res.code == RespOk) {
      ElMessage({ type: "success", message: "已提交保存，请刷新查看结果" });
    }
  });
};
const GenFile = () => {
  SaveEntityInfo();
  HttpSendGenFile().then((res: ResultData<any>) => {
    if (res.code == RespOk) {
      ElMessage({ type: "success", message: "请到文件查看结果" });
    }
  });
};

//实体切换事件
const EntityChange = () => {
  useStoreGeneration().SetCurrEntityName(CurrEntityName.value);
  GetEntityInfo();
};

let refEditField = ref();
const EditFieldClick = (rows: any) => {
  refEditField.value.ShowPop(rows);
};

//更新某个参数的值
const updateFieldParam = (newInfo: FieldInfo) => {
  const fieldList = CurrEntityInfo.value.field_list;
  if (fieldList) {
    for (let i = 0; i < fieldList.length; i++) {
      let item = fieldList[i];
      if (item.field_name == newInfo.field_name) {
        if (CurrEntityInfo.value.field_list) {
          CurrEntityInfo.value.field_list[i] = newInfo;
        }
      }
    }
  }
};
const GetEntityNames = () => {
  HttpSendGenEntityNames().then((res: ResultData) => {
    if (res.code == RespOk) EntityNames.value = res.data;
  });
};

//获取实体详情
const GetEntityInfo = () => {
  let params = { entity_name: CurrEntityName.value } as ReqEntityInfo;
  HttpSendGenEntityInfo(params).then((res: ResultData) => {
    if (res.code == RespOk) {
      CurrEntityInfo.value = res.data;
      CurrFieldList.value = CurrEntityInfo.value.field_list as FieldInfo[];
    }
  });
};
</script>
<style lang="scss" scoped></style>
